package com.fsc.comadapoc1.presentation.panels
{
	import com.fsc.comadapoc1.domain.PortfolioModel;
	import com.fsc.comadapoc1.entities.Portfolio;
	import com.fsc.comadapoc1.events.LoadPortfolioEvent;
	import com.fsc.comadapoc1.events.PanelChangeEvent;
	import com.fsc.comadapoc1.events.PortfolioSearchEvent;
	import com.fsc.comadapoc1.events.SelectionChangeEvent;
	
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	
	import spark.events.ListEvent;
	
	/**
	 * Presentation Model for PortfoliosPanel
	 * 
	 */ 
	
	[Event(name="changeState", type="events.PanelChangeEvent")]
	[Event(name="searchPortfolio", type="events.PortfolioSearchEvent")]
	[Event(name="loadPortfolio", type="events.PortfolioSearchEvent")]
	[ManagedEvents(names="changeState,searchPortfolio,loadPortfolio")]
	public class PortfoliosPanelPM extends EventDispatcher
	{
		
		/** The landing state **/
		public const LANDING_STATE : String = "landingState";
		
		/** The searching state. **/
		public const SEARCHING_STATE : String = "searchingState";
		
		/** The results state. **/
		public const RESULTS_STATE : String = "resultsState";
		
		/** The no results state. **/
		public const NO_RESULTS_STATE : String = "noResultsState";
		
		/**
		 * Keeps track of search term
		 */ 
		private var searchTerm:String;
		
		private var _portfoliosArrColl:ArrayCollection;
		
		[Bindable]
		[Inject]
		public var model:PortfolioModel;
		
		/**
		 * Constructor
		 */
		[MessageHandler(selector="searchPortfolio")]
		public function onSearch(event:PortfolioSearchEvent):void{
			currentState = SEARCHING_STATE;
			trace(currentState);
		}
		
		[MessageHandler(selector="searchComplete")]
		public function onSearchResults(event:PortfolioSearchEvent):void{
			trace("SearchPanelPM: Received ");
	
			if(model.searchResults.length > 0){
				currentState = RESULTS_STATE;
				trace("lots of results")
			}else{
				currentState = NO_RESULTS_STATE;
				trace("no results")
			}
		}
		
		[MessageHandler]
		public function handleStateChange(event:PanelChangeEvent):void{
			
			//send the search to load initial values
			if(event.state == PanelChangeEvent.PORTFOLIOS_VIEW_STATE)
				search("");

		}
		

				
		
		
		private var _currentState:String = LANDING_STATE;
		
		/**
		 * Get the current state of the presentation model.
		 */
		[Bindable]
		public function get currentState() : String
		{
			return _currentState;
		}
		
		/**
		 * Sets the current state of the presentation model.
		 */
		public function set currentState( state:String ) : void
		{
			_currentState = state;	
		}
		
		

		[Bindable]
		public function get portfoliosArrColl():ArrayCollection
		{
			return _portfoliosArrColl;
		}

		public function set portfoliosArrColl(value:ArrayCollection):void
		{
			_portfoliosArrColl = value;
		}
		
		
		/**
		 * Dispatches search events for searchTerm
		 * Calls search
		 */ 		
		public function search(term:String):void{
			searchTerm = term;
			//if search term is not blank
			if(searchTerm != ""){	}
				dispatchEvent(new PortfolioSearchEvent(PortfolioSearchEvent.SEARCH_PORTFOLIO, searchTerm));
				dispatchEvent(new PanelChangeEvent(PanelChangeEvent.SEARCH_STATE));
		
		}
		
		public function portfolioDatagrid_clickHandler(selectedObject:*):void{
			if(selectedObject is Portfolio){
				var portfolioId:String = selectedObject.id;
				trace("PortfolioPanelPM: Selection Detected");
				var po:Portfolio = new Portfolio();
				po.id = selectedObject.id;
				po.name = selectedObject.name;
				po.referenceCurrency = selectedObject.referenceCurrency;
				
				dispatchEvent(new LoadPortfolioEvent(LoadPortfolioEvent.LOAD_PORTFOLIO, po));
				dispatchEvent(new PanelChangeEvent(PanelChangeEvent.PORTFOLIO_VIEW_STATE  ));
			}
			
		}
		
		

	}
}